package bank
{
	import bank.prompt.PromptMsgChestForce;
	import bank.prompt.PromptMsgChestOpen;
	import bank.prompt.PromptMsgChestSave;
	
	import core.Control;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	
	import global.GameState;
	
	import model.load.XmlLoadMessage;
	import flash.ui.Mouse;
	
	public class BankLandControl
	{
		
		private static var _instance:BankLandControl;
		
		public var bankLand:BankLand;
		public var bankLeft:BankLeft;
		private var currentId:String;
		private var currentData:XML;
		
		public function BankLandControl()
		{
			if (_instance)
			{
				throw Error("单列");
			}
		}
		
		public static function getInstance():BankLandControl
		{
			if (_instance == null) _instance = new BankLandControl();
			return _instance;
		}
		
		public function isSelfBank():Boolean
		{
			return currentId == GameState.snsId;
		}
		
		public function loadBankInfo(playerId:String):void
		{
			Mouse.show();
			var loadMsg:XmlLoadMessage = new XmlLoadMessage(onInfoBack);
			loadMsg.url = GameState.getServerLink() + "43&key=" + GameState.snsKey + "&id=" + playerId;
			loadMsg.send();
			this.bankLand.showLoading();
			bankLand.homeBut.visible = false;
		}
		
		private function onInfoBack(xml:XML,id:int,isClearHint:Boolean = true):void
		{
			trace(xml);
			currentData = xml;
			currentId = xml.@id;
			
			this.bankLand.clear(isClearHint);
			this.bankLand.update(xml);
			this.bankLand.hideLoading();
			
			if (currentId != GameState.snsId)
			{
				bankLand.homeBut.visible = true;
			}
			else
			{
				bankLand.homeBut.visible = false;
			}
		}
		//-------------开箱子
		public function openChest(type:int,chest:Chest):void
		{
			if (currentId != GameState.snsId)
			{
				this.bankLand.updateChestHint(type,"还没有激活，等等吧。");
				chest.mouseEnabled = true;
				return;
			}
			
			if (currentData..box.(@type == type)[0].@state == ChestTool.CHEST_STATE_ACTIVATE)
			{
				var price:int = int(currentData.box.(attribute("type") == type)[0].@price);
				if ( price <= 0)
				{
					sendChestOpen(type);
				}
				else
				{
					var msg:PromptMsgChestOpen = new PromptMsgChestOpen(this,type,chest);
					msg.content = "激活箱子要花费" + price + "金，确定吗？";
					Control.getInstance().alertYesNoPanelControl.open(msg);
				}
			}
		}
		
		public function sendChestOpen(type:int):void
		{
			var loadMsg:XmlLoadMessage = new XmlLoadMessage(onOpenChest);
			loadMsg.url = GameState.getServerLink() + "45&key=" + GameState.snsKey + "&type=" + type;
			loadMsg.send();
		}
		
		private function onOpenChest(xml:XML,id:int):void
		{
			var type:int = int(xml.@type);
			var result:int = int(xml.@res);
			var content:String = xml.@desc;
			var leftTime:String = xml.@leftTime;
			var cash:String = xml.@cash;
			currentData.@cash = cash;
			switch(result)
			{
				case 0:
					//content = "已经开启过。";
					break;
				case 1:
					//content = "开启成功。";
					
					currentData.box.(attribute("type") == type)[0].@state = ChestTool.CHEST_STATE_EMPTY;
					currentData.box.(attribute("type") == type)[0].@leftTime = leftTime;
					break;
				case -1:
					//content = "开启金钱不够。";
					break;
				case -2:
					//content = "开启等级不够。";
					break;
			}
			onInfoBack(currentData,-1,false);
			this.bankLand.updateChestHint(type,content);
		}
//-------------------------存钱
		public function saveMoney(type:int,chest:Chest):void
		{
			if (currentId == GameState.snsId)
			{
				var msg:PromptMsgChestSave = new PromptMsgChestSave(this,type,chest);
				msg.content = "确定要存入" + currentData.box.(attribute("type") == type)[0].@money + "个金币吗？";
				Control.getInstance().alertYesNoPanelControl.open(msg);
			}
			else
			{
				//this.bankLand.updateChestHint(type,currentData.box.(attribute("type") == type)[0].@clickDesc);
				this.bankLand.updateChestHint(type,"不能在好友的箱子存储哦。");
				chest.mouseEnabled = true;
			}
			
		}
		
		public function sendSaveMoney(type:int):void
		{
			var loadMsg:XmlLoadMessage = new XmlLoadMessage(onSaveMoney);
			loadMsg.url = GameState.getServerLink() + "46&key=" + GameState.snsKey + "&type=" + type;
			loadMsg.send();
		}
		
		private function onSaveMoney(xml:XML,id:int):void
		{
		
			var type:int = int(xml.@type);
			var result:int = int(xml.@res);
			var content:String = xml.@desc;
			var leftTime:String = xml.@leftTime;
			var cash:String = xml.@cash;
			currentData.@cash = cash;
			switch(result)
			{
				case 0:
				
					break;
				case 1:
					//this.bankLand.updateChestState(type,ChestTool.CHEST_STATE_STORE);
					currentData.box.(attribute("type") == type)[0].@state = ChestTool.CHEST_STATE_STORE;
					currentData.box.(attribute("type") == type)[0].@leftTime = leftTime;
					break;
				
			}
			onInfoBack(currentData,-1,false);
			this.bankLand.updateChestHint(type,content);
		}
		
		//--------------------收获
		public function havestMoney(type:int,chest:Chest):void
		{
			sendHavestMoney(type);
		}
		
		public function sendHavestMoney(type:int):void
		{
			var loadMsg:XmlLoadMessage = new XmlLoadMessage(onHavest);
			loadMsg.url = GameState.getServerLink() + "48&key=" + GameState.snsKey +"&type=" + type + "&id=" + currentId;
			loadMsg.send();
		}
		
		private function onHavest(xml:XML,id:int):void
		{
			var type:int = int(xml.@type);
			var result:int = int(xml.@res);
			var content:String = xml.@desc;
			var state:int = int(xml.@state);
			var leftTime:String = xml.@leftTime;
			var cash:String = xml.@cash;
			var getMoney:int = int(xml.@getMoney);
			
			currentData.@cash = cash;
			if (result == 1)
			{
				//this.bankLand.updateChestState(type,state);
				currentData.box.(attribute("type") == type)[0].@state = state;
				currentData.box.(attribute("type") == type)[0].@leftTime = leftTime;
				currentData.box.(attribute("type") == type)[0].@mouseOnDesc = content;
				
			}
			onInfoBack(currentData,-1,false);
			this.bankLand.updateChestHint(type,content);
			
			if (result == 1)
			{
				this.bankLand.updateChestAnimate(type);
				this.bankLand.updateChestMoneyChange(type,getMoney);
			}
		}
		
		//进入个好友银行
		public function gotFriend(direction:int):void
		{
			loadInfo(direction,currentId);
			bankLand.homeBut.visible = false;
		}
		
		private function loadInfo(direction:int,id:String):void
		{
			currentId = id;
			var loadMsg:XmlLoadMessage = new XmlLoadMessage(onInfoBack);
			loadMsg.url = GameState.getServerLink() + "49&key=" + GameState.snsKey + "&id=" + currentId + "&direct=" + direction;
			loadMsg.send();
			this.bankLand.showLoading();
			
		}
		
		//强行取出
		public function forceGet(type:int,chest:Chest):void
		{
			if (currentId == GameState.snsId)
			{
				var msg:PromptMsgChestForce = new PromptMsgChestForce(this,type,chest);
				msg.content = "收获时间未到，强行取出将失去所有的利息，确定强行取出吗？";
				Control.getInstance().alertYesNoPanelControl.open(msg);
			}
			else
			{
				
				this.bankLand.updateChestHint(type,"还没到收获时间呢。");
				chest.mouseEnabled = true;
				//sendHavestMoney(type);
			}
		}

		
////////////////////////////////////////////////////////////////////
		public function getLevelList():void
		{
			var msg:XmlLoadMessage = new XmlLoadMessage(onLevelListBack);
			msg.url = GameState.getServerLink() + "51&key=" + GameState.snsKey + "&type=0"
			msg.id = 0;
			msg.send();
		}
		
		private function onLevelListBack(xml:XML,id:int):void
		{
			if (id == 0)
			{
				bankLeft.updateLevel(xml);
			}
			else
			{
				bankLeft.updateFriend(xml);
			}
		}
		
		public function getFriendList():void
		{
			var msg:XmlLoadMessage = new XmlLoadMessage(onLevelListBack);
			msg.id = 1;
			msg.url = GameState.getServerLink() + "51&key=" + GameState.snsKey + "&type=1"
			msg.send();
		}
		
		public function refreshList(type:int):void
		{
			if (type == 0)
			{
				getLevelList();
			}
			else
			{
				getFriendList();
			}
		}
//   SEARCH  //////////////////////////////////////////////////////////////////
		
		private var isfirst:Boolean = true;
		
		public function initSearchListener():void
		{
			bankLeft.searchInput.addEventListener(FocusEvent.FOCUS_IN,onFocus);
			bankLeft.searchInput.addEventListener(FocusEvent.FOCUS_OUT,onFocus);
			bankLeft.searchInput.addEventListener(Event.CHANGE,onTextChange);
		}
		
		private function onFocus(event:FocusEvent):void
		{
			if (event.type == FocusEvent.FOCUS_IN)
			{
				if (isfirst)
				{
					isfirst = false;
					bankLeft.searchInput.text = "";
				}
			}
		}
		
		private function onTextChange(event:Event):void
		{
			
		}
	}
}